package com.ideapse.springcloud.controller;

import com.ideapse.springcloud.pojo.Dept;
import com.ideapse.springcloud.service.DeptService;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LuoJianWei
 * @email LuoJianWei_98@foxmail.com
 * @date 2024/4/27 17:02
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    @PostMapping
    public boolean addDept(@RequestBody  Dept dept){
       return deptService.addDept(dept);
    }

    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/{id}")
    public Dept getDeptById(@PathVariable Long id){
        return deptService.getDeptById(id);
    }

    @GetMapping("/all")
    public List<Dept> getAll(){
        return deptService.getAll();
    }

    /**
     * 根据id查询备选方案(熔断)
     * @param id
     * @return
     */
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept().builder().deptno(id)
                .dname("这个id=>"+id+",没有对应的信息,null---@Hystrix~")
                .db_source("在MySQL中没有这个数据库").build();
    }

}
